package DataLayer;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.TreeSet;



import Model.Account;
import Model.Accounts;


public class DaoAccount extends DaoConnection{
	private static DaoAccount single;
		
	public void insertAccount(Account R) {
		try {
			openConn();
			Statement stmt = getConn().createStatement();
			String sql = "insert into Account(accountNumber, type, name, interests, balance) values('"
					+ R.getAccountNumber()
					+ "', '"
					+ R.getType()
					+ "','"
					+ R.getAccountName()
					+ "','"
					+ R.getInterests()
					+ "','"
					+ R.getBalance()
					+"')";
			System.out.println(sql);
			stmt.execute(sql);
			stmt.close();
			closeconn();
		} catch (Exception e) {
			
		}
		return;
	}
	
	public Account getAccount(String accountNumber) {
		Account R = null;
		try {
			openConn();
			Statement stmt = getConn().createStatement();
			String sql = "SELECT accountNumber, type, balance FROM Account";
			
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				System.out.println("Rekening Check");
				R = new Account();
				R.setAccountNumber(rs.getString("accountNumber"));
				R.setType(rs.getString("type"));
				R.setBalance(rs.getBigDecimal("balance").doubleValue());
				System.out.println("Got it");
				System.out.println(R);
			}
			stmt.close();
			closeconn();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R;
	}
	
	public TreeSet<Account> loadAccounts() {

		TreeSet<Account> Acc = new TreeSet<Account>();
		try {
			openConn();
			Statement stmt = getConn().createStatement();
			String sql = "Select * from Account ";
			ResultSet rs = stmt.executeQuery(sql);
			Acc.clear();

			while (rs.next()) {

				Account R = new Account();
				R.setAccountNumber(rs.getString("accountNumber"));
				R.setType(rs.getString("type"));
				R.setBalance(rs.getBigDecimal("balance").doubleValue());

				Acc.add(R);

			}
			 System.out.println(Acc);
			closeconn();
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Acc;

	}

	public static DaoAccount getInstance(){
		if(single == null){
			single = new DaoAccount();
		}
		return single;
	}
	
	public static void selfDestroy(){
		single = null;
	}


}
